package cn.bin758.h5game.module.game.controller.admin.GameData;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.bin758.h5game.framework.common.pojo.PageResult;
import cn.bin758.h5game.framework.common.pojo.CommonResult;
import static cn.bin758.h5game.framework.common.pojo.CommonResult.success;

import cn.bin758.h5game.framework.excel.core.util.ExcelUtils;

import cn.bin758.h5game.framework.operatelog.core.annotations.OperateLog;
import static cn.bin758.h5game.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.bin758.h5game.module.game.controller.admin.GameData.vo.*;
import cn.bin758.h5game.module.game.dal.dataobject.GameData.GameDataDO;
import cn.bin758.h5game.module.game.convert.GameData.GameDataConvert;
import cn.bin758.h5game.module.game.service.GameData.GameDataService;

@Api(tags = "管理后台 - 游戏数据")
@RestController
@RequestMapping("/game/data")
@Validated
public class GameDataController {

    @Resource
    private GameDataService dataService;

    @PostMapping("/create")
    @ApiOperation("创建游戏数据")
    @PreAuthorize("@ss.hasPermission('game:data:create')")
    public CommonResult<String> createData(@Valid @RequestBody GameDataCreateReqVO createReqVO) {
        return success(dataService.createData(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新游戏数据")
    @PreAuthorize("@ss.hasPermission('game:data:update')")
    public CommonResult<Boolean> updateData(@Valid @RequestBody GameDataUpdateReqVO updateReqVO) {
        dataService.updateData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除游戏数据")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('game:data:delete')")
    public CommonResult<Boolean> deleteData(@RequestParam("id") String id) {
        dataService.deleteData(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得游戏数据")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('game:data:query')")
    public CommonResult<GameDataRespVO> getData(@RequestParam("id") String id) {
        GameDataDO data = dataService.getData(id);
        return success(GameDataConvert.INSTANCE.convert(data));
    }

    @GetMapping("/list")
    @ApiOperation("获得游戏数据列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('game:data:query')")
    public CommonResult<List<GameDataRespVO>> getDataList(@RequestParam("ids") Collection<String> ids) {
        List<GameDataDO> list = dataService.getDataList(ids);
        return success(GameDataConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得游戏数据分页")
    @PreAuthorize("@ss.hasPermission('game:data:query')")
    public CommonResult<PageResult<GameDataRespVO>> getDataPage(@Valid GameDataPageReqVO pageVO) {
        PageResult<GameDataDO> pageResult = dataService.getDataPage(pageVO);
        return success(GameDataConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出游戏数据 Excel")
    @PreAuthorize("@ss.hasPermission('game:data:export')")
    @OperateLog(type = EXPORT)
    public void exportDataExcel(@Valid GameDataExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<GameDataDO> list = dataService.getDataList(exportReqVO);
        // 导出 Excel
        List<GameDataExcelVO> datas = GameDataConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "游戏数据.xls", "数据", GameDataExcelVO.class, datas);
    }

}
